package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.INoticeBizService;
import com.kk.xhr.model.entity.Notice;
import com.kk.xhr.web.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 公告管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/19
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "公告管理", tags = "公告管理")
@RequestMapping("/notice")
public class NoticeController {
    private final Executor adminControllerBizExecutor;
    private final INoticeBizService noticeBizService;

    @ApiOperation(value = "查询公告")
    @GetMapping
    public CompletionStage<ApiResult<List<Notice>>> query() {
        return CompletableFuture.supplyAsync(() -> ApiResult.success(noticeBizService.query()), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加公告")
    @PostMapping
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> add(@RequestBody Notice notice) {
        return CompletableFuture.supplyAsync(() -> {
            noticeBizService.add(notice.getContent());
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改公告")
    @PutMapping("/{id:.+}")
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, @RequestBody Notice notice) {
        return CompletableFuture.supplyAsync(() -> {
            noticeBizService.modify(id, notice.getContent());
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "删除公告")
    @DeleteMapping("/{id:.+}")
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id) {
        return CompletableFuture.supplyAsync(() -> {
            noticeBizService.remove(id);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
